package cn.danger.com.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * Created by Administrator on 2016/1/14.
 */
public class MyNetManager {

    private Context context;
    ConnectivityManager manager;

    public MyNetManager(Context context) {
        this.context = context;
    }

    /**
     * 检查网络
     *
     * @return
     */
    public boolean isNetworkAvailable() {
        manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return false;
        } else {
            NetworkInfo[] networkInfo = manager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
                    Log.i("TAG", i + "--status--" + networkInfo[i].getState());
                    Log.i("TAG", i + "--type--" + networkInfo[i].getTypeName());
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
